package com.xbongbong.workorder.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.data.service.RichTextService;
import com.xbongbong.paas.domain.entity.PaasFormEntity;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.OwnerAndCoUserHelp;
import com.xbongbong.paas.pojo.vo.FormDataGetVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.parent.abstracts.AbstractBusinessDetailBaseInfoService;
import com.xbongbong.parent.parse.data.FormDataDetailAnalysisDataHelp;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import com.xbongbong.saas.domain.entity.WorkOrderEntity;
import com.xbongbong.saas.domain.entity.WorkOrderExplainEntity;
import com.xbongbong.saas.domain.entity.ext.WorkOrderFormEntityExt;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.WorkOrderExplainModel;
import com.xbongbong.saas.model.WorkOrderFormModel;
import com.xbongbong.saas.model.WorkOrderModel;
import com.xbongbong.workorder.explain.WorkOrderFormDataFormExplainHandleName;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/02/14 9:07
 */
@Service("workOrderDetailBaseInfoServiceImpl")
public class WorkOrderDetailBaseInfoServiceImpl extends AbstractBusinessDetailBaseInfoService<WorkOrderFormEntityExt, WorkOrderEntity> {
    
    @Resource
    private WorkOrderFormModel workOrderFormModel;
    @Resource
    private WorkOrderModel workOrderModel;
    @Resource
    private WorkOrderExplainModel workOrderExplainModel;
    @Resource
    private WorkOrderFormDataFormExplainHandleName workOrderFormDataFormExplainHandleName;
    @Resource
    private OwnerAndCoUserHelp ownerAndCoUserHelp;
    @Resource
    private FormDataDetailAnalysisDataHelp formDataDetailAnalysisDataHelp;
    @Resource
    private RichTextService richTextService;


    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.WORK_ORDER.getCode());
    }


    @Override
    public WorkOrderFormEntityExt getForm(UserVO loginUser, WorkOrderEntity form, String corpid) throws XbbException {


        return workOrderFormModel.getByKey(form.getFormId(), corpid);
    }

    @Override
    public void handleDetailSpecialExplain( WorkOrderEntity formDataEntity, List<FieldAttrEntity> explainList) throws XbbException {

    }

    @Override
    public FormDataGetVO returnVo(WorkOrderEntity formData, HandlerExplainVO handlerExplainVO, JSONObject data, WorkOrderFormEntityExt form, String userId, String corpid) throws XbbException {
        FormDataGetVO formDataGetVO = new FormDataGetVO();
        // 子表单和tab相关处理
        formDataGetVO.setHeadList(ExplainUtil.getFieldListNoIncludeSubForm(handlerExplainVO).getHeadList());
        PaasFormEntity paasFormEntity = new PaasFormEntity();
        paasFormEntity.setFieldPosition(form.getFieldPosition());
        formDataGetVO.setPaasFormEntity(paasFormEntity);
        formDataGetVO.setData(data);
        return formDataGetVO;
    }

    @Override
    public WorkOrderEntity getData(Integer businessType, Integer subBusinessType, Integer saasMark, String corpid, Long dataId, Long formId) throws XbbException {
        WorkOrderEntity workOrderEntity = workOrderModel.getByKey(dataId, corpid);
        richTextService.addRichTextByDataId(workOrderEntity.getId(), corpid, formId, businessType,
                saasMark, workOrderEntity.getDel(), workOrderEntity.getData());
        return workOrderEntity;
    }

    @Override
    public void checkPermission(WorkOrderFormEntityExt formEntity, WorkOrderEntity paasFormDataEntityExt, UserVO loginUser, Integer businessType) throws XbbException {

    }

    @Override
    public List<FieldAttrEntity> getExplainList(WorkOrderFormEntityExt form, String corpid) throws XbbException {
        WorkOrderExplainEntity workOrderExplainEntity = workOrderExplainModel.getByFormId(form.getId(), corpid);
        if ( Objects.isNull(workOrderExplainEntity) ) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_DATA_NOT_EXIST);
        }
        return JSON.parseArray(workOrderExplainEntity.getExplains(), FieldAttrEntity.class);
    }

    @Override
    public HandlerExplainVO handleExplain(List<FieldAttrEntity> explainList, WorkOrderFormEntityExt formEntity, WorkOrderEntity workOrderEntity, HandlerExplainDTO handlerExplainDTO) throws XbbException {
        handlerExplainDTO.setExplainList(explainList);
        ownerAndCoUserHelp.getOwnerIdAndCoUserId(handlerExplainDTO, handlerExplainDTO.getSaasMark(), handlerExplainDTO.getBusinessType(), workOrderEntity, new HashMap<>(), DelEnum.NORMAL.getDel());
       return workOrderFormDataFormExplainHandleName.handlerExplain(handlerExplainDTO, explainList, formEntity, workOrderEntity);
    }

    @Override
    public JSONObject analysisData(WorkOrderEntity formData, HandlerExplainDTO handlerExplainDTO, HandlerExplainVO handlerExplainVO) throws XbbException {
        List<FieldAttrEntity> list = handlerExplainVO.getHeadList();
        Map<String, FieldAttrEntity> explainMap = new HashMap<>(list.size());
        list.forEach(item->explainMap.put(item.getAttr(), item));
        JSONObject data = commonAnalysisAllData(formData, handlerExplainDTO, handlerExplainVO, list, explainMap);
        data.put(FieldTypeEnum.CLOSETIME.getAlias(), formData.getCloseTime());
        data.put(FieldTypeEnum.ENDTIME.getAlias(), formData.getEndTime());
        SaasAnalysisDataDTO saasAnalysisDataDTO = new SaasAnalysisDataDTO();
        BeanUtil.copyProperties(handlerExplainDTO, saasAnalysisDataDTO);
        workOrderModel.formatData(saasAnalysisDataDTO, data);
        return data;
    }

    @Override
    public void analysisGetOfThousandBits(JSONObject data, List<FieldAttrEntity> explainList, Boolean analysisAmountOfThousandBitsWithOriginalValue) throws XbbException {
        formDataDetailAnalysisDataHelp.analysisGetOfThousandBits(data, explainList, analysisAmountOfThousandBitsWithOriginalValue);
    }
}
